package com.sinosoft.comm.util;


import com.sinosoft.utils.classzz.ClassUtil;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public class PageUtil {
    public PageUtil() {

    }

    /**
     * @param filter
     * @param pageNo
     * @param countPage
     * @param total
     * @return
     */
    public static String createPageStr(Object filter, int pageNo, int countPage, long total) {
        try {
            StringBuffer pagestr = new StringBuffer();
            //String filterName=ClassUtil.getFiledValue(filter, "defaultName").toString();
            String formID = ClassUtil.getFiledValue(filter, "defaultFormId").toString();

            if (total > 0) {
                Integer pageType = Integer.valueOf(ClassUtil.getFiledValue(filter, "pageStrType").toString());
                if (pageType == 0) {
                    astrwd(filter, formID, pagestr, pageNo, countPage, total);

                } else if (pageType == 1) {
                    astrwd1(filter, formID, pagestr, pageNo, countPage, total);
                }
				/*String pageType=ClassUtil.getFiledValue(filter, "pageType").toString();
				if("list".equals(pageType)){
					astrwd(filter, formID, pagestr, countNumber);
				}else if("ht".equals(pageType)){
					astr(filter, formID, pagestr, countNumber);
				}*/
                Field[] fields = ClassUtil.getField(filter);
                for (Field field : fields) {
                    Object obj = ClassUtil.getFiledValue(filter, field.getName());
                    String filedValue = "";
                    if (obj != null) {
                        filedValue = obj.toString();
                    }
					/* if(field.getName().equals("orderFiled") && !StringUtils.isNotNull(filedValue))
					 {
						 pagestr.append("<input type='hidden' value='").append(filedValue).append("' name='").append(filterName).append(".").append(field.getName()).append("' id='").append(field.getName()).append("' />");
					 }*/
                    if (!StringUtils.isEmpty(filedValue)) {
                        // Class<?> c = (Class<?>) (field).getGenericType();
                        //  String sName = (c.getSimpleName());
						/* if ("Date".equals(sName)){
							  	filedValue = DateUtil.changeUsToChina(filedValue);
						  }*/
                        pagestr.append("<input type='hidden' value='").append(filedValue).append("' name='").append(field.getName()).append("' id='").append(field.getName()).append("' />");
                    }
                }

            } else {
                pagestr.append("");
            }
            return pagestr.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static void astrwd(Object filter, String formID, StringBuffer pagestr, int pageNo, int countPage, long total) {
        //int[] pageNums={10,15,20,25,30,40,50,100};
        //每页多少条
        //int pageSize=pageInfo.getPageSize();
        //当前第几页
		/* int pageNo=pageInfo.getPageNum();
		 int countPage=pageInfo.getPages();*/
        if (total > 0) {
            long upcount = 0;
            if (pageNo < 6) {
                upcount = 6 - pageNo;
            }
            long downcount = 0;
            if (countPage - (pageNo + 5) < 0) {
                downcount = countPage - (pageNo + 5);
            }
            pagestr.append(" <li ><a style=\"margin-right: 5px;\" href=\"#\">共").append(total).append("条/").append(countPage).append("页</a></li>");
            if (countPage <= 1 || countPage == 1 || pageNo - 1 == 0) {
                pagestr.append("<li class=\"prev disabled\"> <a href=\"#\"><i class=\"ace-icon fa fa-angle-double-left\"></i></a></li>");
            } else {
                pagestr.append("<li class=\"prev\"><a href=\"#\"  onclick=\"go(").append(pageNo - 1).append(")\" ><i class=\"ace-icon fa fa-angle-double-left\"></i></a></li>");
            }

            if (countPage >= 1) {
                if (pageNo == 1) {
                    pagestr.append("<li class=\"active\"><a  href=\"#\">1</a><li>");
                } else {
                    pagestr.append("<li><a href=\"#\"  onclick=\"go('1')\">1</a></li>");
                }
            }

            for (int i = (int) (4 - downcount); i > 0; i--) {
                if (pageNo - i >= 2) {
                    if (i == 4 - downcount && pageNo - i > 2) {
                        pagestr.append("<li><a href=\"#\">...</a></li>");
                    }
                    pagestr.append("<li><a href=\"#\" onclick=\"go(").append(pageNo - i).append(")\" >").append(pageNo - i).append("</a></li>");
                }
            }

            if (pageNo > 1 && pageNo < countPage) {
                pagestr.append("<li class=\"active\"><a   href=\"#\">").append(pageNo).append("</a></li>");
            }
            for (int i = 1; i < 5 + upcount; i++) {
                //System.out.println(cpage-i);
                if (pageNo + i < countPage) {
                    pagestr.append("<li><a href=\"#\" onclick=\"go(").append(pageNo + i).append(")\" >").append(pageNo + i).append("</a></li>");
                }

                if (i == (5 + upcount) - 1 && pageNo + i < countPage - 1) {
                    pagestr.append("<li><a href=\"#\">...</a></li>");
                }
            }

            if (countPage == pageNo) {
                if (countPage > 1) {
                    pagestr.append("<li class=\"active\"><a  href=\"#\" >").append(countPage).append("</a></li>");
                }
                pagestr.append("<li class=\"next disabled\"><a href=\"#\" ><i class=\"ace-icon fa fa-angle-double-right\"></i></a></li>");
            } else {
                if (countPage > 1) {
                    pagestr.append("<li><a href=\"#\" onclick=\"go(").append(countPage).append(")\">").append(countPage).append("</a></li>");
                }
                pagestr.append("<li class=\"next\"><a href=\"#\"  onclick=\"go(").append(pageNo + 1).append(")\"><i class=\"ace-icon fa fa-angle-double-right\"></i></a></li>");
            }
            //pagestr.append(" <li>转到第<input type='text' id='tPageNo'  value="+pageNo+" /></li>");
            //pagestr.append(" <li class='sub_go'><a href='###' onclick=\"go(document.getElementById('tPageNo').value)\" >GO</a></li>");
            scripstr(formID, pagestr);
        }
    }


    private static void astrwd1(Object filter, String formID, StringBuffer pagestr, int pageNo, int countPage, long total) {
		/* int pageNo=pageInfo.getPageNum();
		 int countPage=pageInfo.getPages();*/
        if (total > 0) {
            long upcount = 0;
            if (pageNo < 6) {
                upcount = 6 - pageNo;
            }
            long downcount = 0;
            if (countPage - (pageNo + 5) < 0) {
                downcount = countPage - (pageNo + 5);
            }
            if (countPage <= 1 || countPage == 1 || pageNo - 1 == 0) {
                pagestr.append("<li class=\"lf\"> <a href=\"javascript:;\">上一页</a></li>");
            } else {
                pagestr.append("<li class=\"lf\"><a href=\"javascript:;\"  onclick=\"go(").append(pageNo - 1).append(")\" >上一页</a></li>");
            }

            if (countPage >= 1) {
                if (pageNo == 1) {
                    pagestr.append("<li class=\"lf on_li\"><a  href=\"javascript:;\">1</a></li>");
                } else {
                    pagestr.append("<li class=\"lf\" ><a href=\"javascript:;\"  onclick=\"go('1')\">1</a></li>");
                }
            }

            for (int i = (int) (4 - downcount); i > 0; i--) {
                if (pageNo - i >= 2) {
                    if (i == 4 - downcount && pageNo - i > 2) {
                        pagestr.append("<li class=\"lf\" ><a href=\"javascript:;\">...</a></li>");
                    }
                    pagestr.append("<li class=\"lf\"><a href=\"javascript:;\" onclick=\"go(").append(pageNo - i).append(")\" >").append(pageNo - i).append("</a></li>");
                }
            }

            if (pageNo > 1 && pageNo < countPage) {
                pagestr.append("<li class=\"lf on_li\"><a   href=\"javascript:;\">").append(pageNo).append("</a></li>");
            }
            for (int i = 1; i < 5 + upcount; i++) {
                //System.out.println(cpage-i);
                if (pageNo + i < countPage) {
                    pagestr.append("<li class=\"lf\" ><a href=\"javascript:;\" onclick=\"go(").append(pageNo + i).append(")\" >").append(pageNo + i).append("</a></li>");
                }

                if (i == (5 + upcount) - 1 && pageNo + i < countPage - 1) {
                    pagestr.append("<li class=\"lf\" ><a href=\"javascript:;\">...</a></li>");
                }
            }
            if (countPage == pageNo) {
                if (countPage > 1) {
                    pagestr.append("<li class=\"lf on_li\"><a  href=\"javascript:;\" >").append(countPage).append("</a></li>");
                }
                pagestr.append("<li class=\"lf\"><a href=\"#\" >下一页</a></li>");
            } else {
                if (countPage > 1) {
                    pagestr.append("<li class=\"lf\" ><a href=\"javascript:;\" onclick=\"go(").append(countPage).append(")\">").append(countPage).append("</a></li>");
                }
                pagestr.append("<li class=\"lf\"><a href=\"javascript:;\"  onclick=\"go(").append(pageNo + 1).append(")\">下一页</a></li>");
            }
            scripstr(formID, pagestr);
        }
    }

    private static void scripstr(String formID, StringBuffer pagestr) {
        pagestr.append("<script>function go(cpage){ document.getElementById('page').value=cpage;document.getElementById('").append(formID).append("').submit();}");
        pagestr.append("function goIpage(ipage){document.getElementById('page').value=1; document.getElementById('rows').value=ipage;document.getElementById('").append(formID).append("').submit();}");
        pagestr.append("</script>");
    }


}
